<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>
        用户管理
        <span v-if="usernun" style="font-size: 12px"
          >您的账户授权子用户数为<span style="color: red">{{ usernun }}</span
          >个, 您还可以增加<span style="color: red">{{ countnun }}</span
          >个子用户.</span
        >
				</span>
      </div>
			
			

      <!-- 搜索表单 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <div class="app-container">
          <div class="filter-container">
            <el-input
              v-model="where.username"
              class="filter-item"
              clearable
              placeholder="用户账户"
              style="width: 180px"
              @clear="reload"
            />

            <el-input
              v-model="where.nickname"
              class="filter-item"
              clearable
              placeholder="用户昵称"
              style="width: 180px"
              @clear="reload"
            />

            <el-select
              v-model="where.role"
              class="filter-item"
              clearable
              placeholder="请选择角色组"
              @change="reload"
              @clear="reload"
            >
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
              />
            </el-select>

            <el-button
              class="filter-item"
              icon="el-icon-search"
              type="primary"
              @click="reload"
              >搜索
            </el-button>

            <el-button
              class="filter-item"
              icon="el-icon-refresh"
              style="margin-left: 2px"
              @click="reset"
              >重置
            </el-button>
          </div>
        </div>
      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :selection.sync="selection"
        :where="where"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            v-if="$hasPermission('user:save')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openEdit({})"
            >添加
          </el-button>
          <el-button
            v-if="$hasPermission('user:delete')"
            icon="el-icon-delete"
            size="small"
            type="danger"
            @click="removeBatch"
            >删除
          </el-button>
        </template>
        <!-- 角色列 -->
        <template slot="role" slot-scope="{ row }">
          <el-tag
            v-for="item in row.roles"
            :key="item.id"
            :disable-transitions="true"
            size="mini"
            type="primary"
          >
            {{ item.roleName }}
          </el-tag>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="editState(row)"
          />
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            v-if="$hasPermission('user:update')"
            :underline="false"
            icon="el-icon-edit-outline"
            type="primary"
            @click="openEdit(row)"
            >修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此用户吗？"
            @confirm="remove(row)"
          >
            <el-link
              v-if="$hasPermission('user:delete')"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              type="danger"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <vue-edit
      :data="current"
      :visible.sync="showEdit"
      @childFn="getRoles"
      @done="reload"
    />
  </div>
</template>

<script>
import VueEdit from "./vue-edit";

export default {
  name: "SystemUser",
  components: { VueEdit },
  data() {
    return {
      // 表格高度
      defaultHeight: 0,
      // 表格数据接口
      url: "/user/read",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
        },
        {
          columnKey: "index",
          type: "index",
          width: 60,
          label: "序号",
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "username",
          label: "用户账户",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: "nickname",
          label: "用户名",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          columnKey: "role",
          label: "用户角色",
          showOverflowTooltip: true,
          minWidth: 110,
          slot: "role",
        },
        {
          prop: "datatime",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          prop: "lasttime",
          label: "最后登录时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          prop: "status",
          label: "状态",
          align: "center",
          sortable: "custom",
          width: 100,
          resizable: false,
          slot: "status",
        },
        {
          columnKey: "action",
          label: "操作",
          width: 130,
          resizable: false,
          slot: "action",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 角色列表
      roleList: [],
      // 是否显示编辑弹窗
      showEdit: false,
      usernun: 0,
      countnun: 0,
    };
  },
  created() {
    this.getUseNun();
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    getUseNun() {
      this.$http
        .get("/user/getUserCount")
        .then((res) => {
          if (res.data.code == 0) {
            if (res.data.usernun > 0) {
              this.usernun = res.data.usernun;
              this.countnun = res.data.usernun - res.data.countnun;
            }
          }
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },

    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /* 获取角色组 */
    getRoles(data) {
      this.roleList = data;
    },
    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.getUseNun();
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .delete("/user/delete/id/" + row.id)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的用户吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          const iad = this.selection.map((d) => d.id);
          const loading = this.$loading({ lock: true });
          this.$http
            .delete("/user/delete", {
              data: { id: iad.join(",") },
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 更改状态 */
    editState(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .put("/user/update/state/" + row.id, { status: row.status })
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.state = !row.state ? 1 : 0;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style scoped>
</style>
